<?php
/**
 * @author Mazvv
 * @package Callsstatuses
 */
class Callsstatuses extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'callsstatuses';
		$this->lang->load('callsstatuses');
		$this->load->model('callsstatuses_model');
		$this->SetNewToolSize('600x200');
		$this->SetEditToolSize('600x200');
		$this->SetDetailsToolSize('600x200');
		$this->SetFindToolSize('600x200');
		
		$this->a_validation_rules['code'] = "required";
		$this->a_validation_rules['status_order'] = "required";
		$this->a_validation_rules['callsstatuses_name'] = "required";
		
		$this->a_validation_fields['code'] = $this->lang->line('CODE');
		$this->a_validation_fields['status_order'] = $this->lang->line('ORDER');
		$this->a_validation_fields['callsstatuses_name'] = $this->lang->line('NAME');
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_Name){
		switch ($m_Name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		$content['code'] =  $this->lang->line('CODE');
		$content['status_order'] =  $this->lang->line('ORDER');
		$content['callsstatuses_name'] =  $this->lang->line('NAME');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->callsstatuses_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if(!$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('callsstatuses/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:'');
			$content['status_order'] =  $this->lang->line('ORDER');
			$content['status_order_field'] =  form_input('status_order', $this->validation->status_order?$this->validation->status_order:'');
			$content['callsstatuses_name'] =  $this->lang->line('NAME');
			$content['callsstatuses_name_field'] =  form_input('callsstatuses_name', $this->validation->callsstatuses_name?$this->validation->callsstatuses_name:'');
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:'');
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			return $this->load->view('callsstatuses/create', $content, True);
		}
		if($insRid = $this->callsstatuses_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->callsstatuses_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:$content['DS']->code);
			$content['status_order'] =  $this->lang->line('ORDER');
			$content['status_order_field'] =  form_input('status_order', $this->validation->status_order?$this->validation->status_order:$content['DS']->status_order);
			$content['callsstatuses_name'] =  $this->lang->line('NAME');
			$content['callsstatuses_name_field'] =  form_input('callsstatuses_name', $this->validation->callsstatuses_name?$this->validation->callsstatuses_name:$content['DS']->callsstatuses_name);
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			return $this->load->view('callsstatuses/edit', $content, True);
		}
		if($this->callsstatuses_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->callsstatuses_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			$content['code'] =  $this->lang->line('CODE');
			$content['code_field'] =  form_input('code', $this->validation->code?$this->validation->code:$content['DS']->code);
			$content['status_order'] =  $this->lang->line('ORDER');
			$content['status_order_field'] =  form_input('status_order', $this->validation->status_order?$this->validation->status_order:$content['DS']->status_order);
			$content['callsstatuses_name'] =  $this->lang->line('NAME');
			$content['callsstatuses_name_field'] =  form_input('callsstatuses_name', $this->validation->callsstatuses_name?$this->validation->callsstatuses_name:$content['DS']->callsstatuses_name);
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			return $this->load->view('callsstatuses/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->callsstatuses_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
}

?>